package com.celebriosoftware.client;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.celebriosoftware.celebrioclient.R;

public class BrowserWebView extends WebView {

	private Activity browserActivity;

	public BrowserWebView(Context context) {
		super(context);
		browserActivity = (Activity)context;
	}

	public BrowserWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
		browserActivity = (Activity)context;
	}

	public BrowserWebView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		browserActivity = (Activity)context;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_MOVE) {
			int historySize = event.getHistorySize();
			int newY = (int) event.getY();
			if (historySize > 0) {
				int oldY = (int) event.getHistoricalY(historySize - 1);
				
				//here the fun begins
				int amount = 6*(oldY - newY); //how much we scroll
				RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
						RelativeLayout.LayoutParams.WRAP_CONTENT,
						RelativeLayout.LayoutParams.WRAP_CONTENT);
				int newUp = this.getTop() - amount; //new top margin size
				//shifting up
				if(amount>0) {
					//scrolled totally down - increase bottom margin now
					int currentBottomMargin = ((RelativeLayout.LayoutParams)browserActivity.findViewById(R.id.web_browser).getLayoutParams()).bottomMargin;
					int bottomLimit = ((LinearLayout) browserActivity.findViewById(R.id.bottomBar)).getHeight() + 3;
					if(computeVerticalScrollOffset() + computeVerticalScrollExtent() >= computeVerticalScrollRange() //scrolled to max down
							&& currentBottomMargin < bottomLimit) { //should move up
						params.setMargins(0, getTop(), 0, Math.min(currentBottomMargin+amount, bottomLimit));
						this.setLayoutParams(params);
						return true;
					}
					//moved up, now native scrolling
					if (this.getTop() <= 0) { 
						return super.onTouchEvent(event);
					}
					if(currentBottomMargin < bottomLimit) {
						//moving up - decreasing top margin
						params.setMargins(0, Math.max(0, newUp), 0, 0);
						this.setLayoutParams(params);
						return true;
					}
				}
				//shifting down
				if(amount<=0) {
					//fully scrolled down, now moving down
					int topLimit=browserActivity.findViewById(R.id.middleBar).getBottom()+4;
					if(computeVerticalScrollOffset() == 0 && this.getTop() < topLimit) {
						params.setMargins(0, Math.min(newUp, topLimit), 0, 0);
						this.setLayoutParams(params);
						return true;
					}
					//moving down - decreasing bottom margin before starting to scroll
					int botm = ((RelativeLayout.LayoutParams)browserActivity.findViewById(R.id.web_browser).getLayoutParams()).bottomMargin;
					if(botm > 0) {
						params.setMargins(0, getTop(), 0, Math.max(botm+amount, 0));
						this.setLayoutParams(params);
						return true;							
					}
					//regular scrolling the page down
					return super.onTouchEvent(event);
				}
				
				//return super.onTouchEvent(event);//return false;
			}
			return super.onTouchEvent(event);//prevents more browser jump glitches
		} else {
			return super.onTouchEvent(event);
		}
	}
	
	public void arrowScrollDown(int amount) {
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		int newUp = this.getTop() - amount; //new top margin size
		//scrolled totally down - increase bottom margin now
		int currentBottomMargin = ((RelativeLayout.LayoutParams)browserActivity.findViewById(R.id.web_browser).getLayoutParams()).bottomMargin;
		int bottomLimit = ((LinearLayout) browserActivity.findViewById(R.id.bottomBar)).getHeight() + 3;
		if(computeVerticalScrollOffset() + computeVerticalScrollExtent() >= computeVerticalScrollRange() //scrolled to max down
				&& currentBottomMargin < bottomLimit) { //should move up
			params.setMargins(0, getTop(), 0, Math.min(currentBottomMargin+amount, bottomLimit));
			this.setLayoutParams(params);
			return;
		}
		//moved up, now native scrolling
		if (this.getTop() <= 0) {
			if(computeVerticalScrollOffset() + computeVerticalScrollExtent() < computeVerticalScrollRange()) {
				scrollBy(0, amount);
			}
			return;
		}
		if(currentBottomMargin < bottomLimit) {
			//moving up - decreasing top margin
			params.setMargins(0, Math.max(0, newUp), 0, 0);
			this.setLayoutParams(params);
			return;
		}
	}
	
	public void arrowScrollUp(int amount) {
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		int newUp = this.getTop() - amount; //new top margin size	
		//fully scrolled down, now moving down
		int topLimit=browserActivity.findViewById(R.id.middleBar).getBottom()+4;
		if(computeVerticalScrollOffset() == 0 && this.getTop() < topLimit) {
			params.setMargins(0, Math.min(newUp, topLimit), 0, 0);
			this.setLayoutParams(params);
			return;
		}
		//moving down - decreasing bottom margin before starting to scroll
		int botm = ((RelativeLayout.LayoutParams)browserActivity.findViewById(R.id.web_browser).getLayoutParams()).bottomMargin;
		if(botm > 0) {
			params.setMargins(0, getTop(), 0, Math.max(botm+amount, 0));
			this.setLayoutParams(params);
			return;
		}
		//regular scrolling the page down
		if(computeVerticalScrollOffset()>0) {
			scrollBy(0, amount);
		}
	}
}